package com.mxt.controller;

import com.github.pagehelper.PageInfo;
import com.mxt.domain.Result;
import com.mxt.domain.User;
import com.mxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/queryAll")
    public List<User> queryAll(){
        return userService.queryAll();
    }

    /**
     * 分页查询
     * @param cid 性别
     * @param keyword 姓名
     * @param pageNumber 页码
     * @param pageSize 每页几条
     * @return
     *//*
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public PageInfo<User> pageQuery(@RequestParam(required = false) Integer cid,
                                     @RequestParam(required = false) String keyword,
                                     @RequestParam(defaultValue = "1") Integer pageNumber,
                                     @RequestParam(defaultValue = "5") Integer pageSize){
        return userService.queryAll(cid, keyword, pageNumber, pageSize);
    }*/

    //保存
    @RequestMapping("/save")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return new Result(true, "保存成功");
    }

    //根据id查询
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User findById(@PathVariable("id") Integer id) {
        return userService.findById(id);
    }

    //修改
    @RequestMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return new Result(true, "修改成功");
    }

    //删除
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Integer id) {
        userService.delete(id);
        return new Result(true, "删除成功");
    }
}
